package edu.example.student.servlet;

import edu.example.common.PowerCheck;
import edu.example.student.beans.Building;
import edu.example.student.beans.Student;
import edu.example.student.dao.BuildingDao;
import edu.example.student.dao.StudentDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/studentlist")
public class StudentListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession(false);
        if (session == null) {
            session = req.getSession();
        }

        Object stu_pages = session.getAttribute("stu_pages");
        Object stu_pageNum = session.getAttribute("stu_pageNum");
        Integer pages = 1;
        Integer pageNum = 20;

        if (stu_pages != null) {
            pages = (Integer) stu_pages;
        }

        if (stu_pageNum != null) {
            pageNum = (Integer) stu_pageNum;
        }

        pages = (pages-1)*pageNum;
        StudentDao studentDao=new StudentDao();
        req.setAttribute("studentlist", studentDao.getList(pages, pageNum));
        BuildingDao buildingDao = new BuildingDao();
        List<Building> all = buildingDao.findAll();
        req.setAttribute("builds", all);
        req.getRequestDispatcher("/student/list.jsp").forward(req,resp);
    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession(false);
        if (session == null) {
            session = req.getSession();
        }

        Object stu_pages = session.getAttribute("stu_pages");
        Object stu_pageNum = session.getAttribute("stu_pageNum");
        Integer pages = 0;
        Integer pageNum = 20;

        if (stu_pages != null) {
            pages = (Integer) stu_pages;
        }

        if (stu_pageNum != null) {
            pageNum = (Integer) stu_pageNum;
        }

        pages = pages*pageNum;
        StudentDao studentDao=new StudentDao();
        req.setAttribute("studentlist", studentDao.getList(pages, pageNum));//查询所有的学生信息
        BuildingDao buildingDao = new BuildingDao();
        List<Building> all = buildingDao.findAll();
        req.setAttribute("builds", all);//查询所有的楼号
         String buildings=req.getParameter("buildingcards");
//         if (buildings == null || buildings.isBlank()) {
//             req.setAttribute("studentlist", studentDao.getList(pages, pageNum));//查询所有的学生信息
//             req.getRequestDispatcher("/student/list.jsp").forward(req,resp);
//             return ;
//         }
        String selectors= req.getParameter("selectors");
        Integer s= Integer.valueOf(selectors);
        String select_content=req.getParameter("select_content");
        switch (s) {
            case 0:
                List<Student> sel_building=studentDao.selbuidling(buildings);
                req.setAttribute("studentlist",sel_building);//根据宿舍楼查询学生的信息
                break;
            case 1:
                List<Student> stu_select=studentDao.selectList(buildings,select_content);
                req.setAttribute("studentlist",stu_select);//根据输入的信息查询(名字)
                break;
            case 2:
                List<Student> stu_sex=studentDao.selsex(buildings,select_content);
                req.setAttribute("studentlist",stu_sex);//根据输入的信息查询（性别）
                break;
            case 3:
                List<Student> stu_rooms=studentDao.selroom(buildings,select_content);
                req.setAttribute("studentlist",stu_rooms);//根据输入的信息查询（寝室号）
                break;
        }
        //默认查询第一页，需要两个参数，当前页码pageIndex,每页展示的条数

        req.getRequestDispatcher("/student/list.jsp").forward(req,resp);
    }
}
